package com.turing.usdtpay.base;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;


/**
 * Created by $Jay
 * Date : 2019\2\13 0013
 **/
public abstract class BaseDialog<T extends BaseDialog<T>> extends Dialog implements View.OnClickListener {

    protected Context mContext;
    protected Activity mActivity;
    public BaseDialog(Context context){
        super(context);
        setDialogTheme();
        this.mContext = context;
        this.mActivity = (Activity) context;
        setCanceledOnTouchOutside(true);
    }

    public BaseDialog(Context context, boolean isPopupStyle) {
        this(context);
    }

    public BaseDialog(Context context, int theme){
        super(context,theme);
        this.mActivity = (Activity) context;
        this.mContext = context;
        setCanceledOnTouchOutside(true);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Window window = getWindow();
        if (null != window) {
//            if (DeviceUtils.isFullScreen(TSdkCore.getInstance().mainActivity)) {
//                window.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
//            } else {
//                window.clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
//            }
            window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);
        }

        View view = onCreateView();
        setContentView(view);
    }

    /**
     * set dialog theme(设置对话框主题)
     */
    private void setDialogTheme() {
        requestWindowFeature(Window.FEATURE_NO_TITLE);// android:windowNoTitle
        getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));// android:windowBackground
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);// android:backgroundDimEnabled默认是true的
//        getWindow().setFlags(LayoutParams.FLAG_FULLSCREEN, LayoutParams.FLAG_FULLSCREEN);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
    }

    /**
     * when dailog attached to window,set dialog width and height and showFlowBall anim
     * (当dailog依附在window上,设置对话框宽高以及显示动画)
     */
    @Override
    public void onAttachedToWindow() {
        super.onAttachedToWindow();
        setUiBeforeShow();
    }

    /**
     * 创建显示页面，并返回容器
     * @return
     */
    public abstract View onCreateView();

    /**
     * set Ui data or logic opreation before attatched window(在对话框显示之前,设置界面数据或者逻辑)
     */
    public abstract void setUiBeforeShow();

    /**
     * 显示一个短Toast
     *
     * @param text
     */
    protected void showShortToast(CharSequence text) {
        if (null != mActivity) {
            Toast.makeText(mActivity, text, Toast.LENGTH_SHORT).show();
        }
    }
}
